/**
 * Created by dongchen on 15/12/16.
 */
var express = require('express');
var router = express.Router();
var co = require('co');
var util = require('util');
var _ = require('lodash');

/**
 * 展示登录页面
 */
router.get('/', function(req, res) {

    var user = req.session.user;
    if(user){
        res.redirect('/admin');
    }else{
        res.render('login', req.session.tips)
    }
});

/**
 * 登录逻辑
 */
router.post('/', function(req, res){

    var name = req.body['userName'];
    var pwd = req.body['pwd'];

    var err = '';
    if(name !== 'hxtxadmin'){
        err = "用户名错误";
    }else if(pwd !== 'hxtxwifi'){
        err = "密码错误";
    }

    if(err){
        //login fail
        req.session.tips = {
            msg : err,
            userName : name,
            pwd : pwd
        };
        res.redirect('/login');
    }else{
        //阅卷用户登录成功
        req.session.user = {
            name : name
        };

        res.redirect('/admin');
    }
});

/**
 * 登出逻辑
 */
router.get('/out', function(req, res){

    delete req.session.user;

    res.redirect('/login');
});

module.exports = router;
